$import("com.hh.workflow.designer.WorkFlowListTree");
Ext.define('com.hh.workflow.start.StartList', {
	extend : 'com.hh.global.SimpleGridPanelWindow',
	action : 'workflow-Start-',
	// editPage : 'com.hh.usersystem.user.UserEdit',
	constructor : function(config) {
		this.config = config || {};
		this.superclass.constructor.call(this, this.config);
		this.add(this.createWorkFlowListTree());
	},
	getGridConfig : function() {
		return {
			title : "我的流程"
		};
	},
	getTbars : function() {
		var page = this;
		this.workflowName = Ext.create('Ext.form.field.Text', {
					fieldLabel : '流程名称',
					labelWidth : 70,
					width : 350
				});
		return [{
					xtype : 'toolbar',
					dock : 'top',
					items : [this.workflowName, this.getMyToolbarItem("start")]
				}];
	},
	getTbarItems : function() {
		var page = this;
		return [this.getMyToolbarItem("select"), this.getMyToolbarItem("end"),
				this.getToolbarItem("delete"),
				this.getMyToolbarItem("selectAll")];
	},
	getMyToolbarItem : function(type) {
		var page = this;
		if (type == 'start') {
			return {
				iconCls : 'add',
				text : '新建工作',
				handler : function() {
					page.doStart();
				}
			};
		} else if (type == 'end') {
			return {
				iconCls : 'delete2',
				text : '终止',
				handler : function() {
					page.doEnd();
				}
			};
		} else if (type == 'select') {
			return {
				iconCls : 'search',
				text : '查看历史',
				handler : function() {
					page.doSelectHi();
				}
			};
		} else if (type == 'selectAll') {
			return {
				iconCls : 'search',
				text : '查看所有流程',
				handler : function() {
					page.doSelectAll();
				}
			};
		}
	},
	doSelectAll : function() {
		var page = this;
		Ext.apply(page.grid.getStore().proxy.extraParams, {
					pdkey : null
				});
		page.grid.getStore().load();
	},
	getContainerRightMenuItems : function() {
		var toolbarItems = [];
		toolbarItems.push(this.getMyToolbarItem("start"));
		toolbarItems.push(this.getMyToolbarItem("selectAll"));
		return toolbarItems;
	},
	getRightMenuItems : function() {
		var toolbarItems = [];
		toolbarItems.push(this.getMyToolbarItem("start"), this
						.getMyToolbarItem("select"), this
						.getMyToolbarItem("end"),
				this.getToolbarItem("delete"), this
						.getMyToolbarItem("selectAll"));
		return toolbarItems;
	},
	doSelectHi : function() {
		var record = this.getGridRecord();
		if (record == null) {
			ExtFrame.info("请选中您要查看历史的流程！！");
			return;
		}
		ExtUtil.open('com.hh.workflow.task.HiTaskList', {
					parentPanel : this,
					extraParams : {
						piid : record.get("id"),
						pdid : record.get("pdid")
					}
				});
	},
	doEnd : function() {
		var records = this.getGridRecords();
		var isDelete = true;
		for (var i = 0; i < records.length; i++) {
			if (records[i].get("endTime")) {
				isDelete = false;
				break;
			}
		}
		if (isDelete) {
			this.doDelete("end", '您确定要终止流程吗？');
		} else {
			ExtFrame.msg("您选中的流程已经结束了，不能终止！");
		}

	},
	doStart : function() {
		var page = this;
		if (Util.isNull(this.pdkey)) {
			ExtFrame.info("请选中您要新建的流程！！");
		} else {
			var result = Request.synRequestObject('workflow-Start-start', {
						pdkey : this.pdkey,
						workflowName : page.workflowName.getValue()
					});
			if (result.href) {
				var params = {};

				var callbackRefresh = function() {
					page.grid.getStore().load();
				};

				Ext.apply(params, Ext.decode(result.hrefParams));
				params.objectId = result.objectId;
				params.taskId = result.taskId;
				params.piid = result.piid;
				params.pdid = result.pdid;
				params.activityId = result.activityId;
				params.title = result.piName + "（" + result.name + "）";
				params.piName = result.piName;
				params.actName = result.name;
				params.callbackRefresh = callbackRefresh;
				params.actConfig = result;
				params.actionType = "manager";
				Desktop.openWindow(result.href, params);
			}
		}
	},
	getGridColumns : function() {
		return [{
					dataIndex : 'pdid',
					hidden : true,
					flex : 1
				}, {
					text : '状态',
					dataIndex : 'endTime',
					width : 50,
					renderer : function(value) {
						if (Util.isNull(value)) {
							return '<font color=green>执行中</font>'
						} else {
							return '<font color=red>已结束</font>'
						}
					}
				}, {
					text : '流程标识',
					dataIndex : 'id',
					flex : 1
				}, {
					text : '流程名称',
					dataIndex : 'piName',
					width : 300
				}, {
					text : '流程开始时间',
					dataIndex : 'startTime',
					width : 200,
					renderer : Ext.util.Format.dateRenderer('Y年m月d日 H时i分s秒')
				}, {
					text : '流程结束时间',
					dataIndex : 'endTime',
					width : 200,
					renderer : Ext.util.Format.dateRenderer('Y年m月d日 H时i分s秒')
				}, {
					text : '耗时',
					dataIndex : 'durationInMillis',
					width : 120
				}];
	},
	getStoreFields : function() {
		return ['id', 'pdid', 'startTime', 'endTime', 'piName',
				'durationInMillis'];
	},
	createWorkFlowListTree : function() {
		var page = this;
		var workFlowListTree = this.workFlowListTree = Ext.create(
				'com.hh.workflow.designer.WorkFlowListTree', {
					editPage : null,
					getTreePanelListeners : function() {
						return {
							itemclick : function(view, record, item) {
								if (record.get('leaf') == false) {
									return;
								}
								page.workflowName.setValue(record.get("text")
										+ " " + Util.getDateStr() + " "
										+ static_var.loginuser.text);
								page.grid.setTitle("我的流程" + "<font color=red>（"
										+ record.get("text") + "）</font>");
								var object = Request.synRequestObject(
										'workflow-Start-findWorkFlowByDataId',
										{
											dataId : record.get("id")
										});
								if (object == null) {
									page.grid.getStore().removeAll();
								} else {
									page.pdkey = object.id;
									Ext
											.apply(
													page.grid.getStore().proxy.extraParams,
													{
														pdkey : page.pdkey
													});
									page.grid.getStore().load();
								}
							}
						}
					}
				});
		return workFlowListTree;
	}
});